<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="grid-lines"></div>
      <div class="floating-particles">
        <div class="particle" v-for="n in 20" :key="n"></div>
      </div>
    </div>
    
    <!-- 主标题 -->
    <div class="login-header">
      <h1 class="system-title">短期电力负荷AI预测分析系统</h1>
      <p class="system-subtitle">Power Load Forecasting System</p>
    </div>
    
    <!-- 登录表单 -->
    <div class="login-form-wrapper">
      <div class="login-form">
        <div class="form-header">
          <div class="login-icon">
            <i class="icon-user"></i>
          </div>
          <h2 class="form-title">系统登录</h2>
        </div>
        
        <form @submit.prevent="handleLogin">
          <div class="input-group">
            <div class="input-wrapper">
              <i class="input-icon icon-username"></i>
              <input 
                type="text" 
                v-model="loginForm.username" 
                placeholder="请输入用户名"
                class="form-input"
                required
              />
            </div>
          </div>
          
          <div class="input-group">
            <div class="input-wrapper">
              <i class="input-icon icon-password"></i>
              <input 
                :type="showPassword ? 'text' : 'password'" 
                v-model="loginForm.password" 
                placeholder="请输入密码"
                class="form-input"
                required
              />
              <i 
                class="toggle-password" 
                :class="showPassword ? 'icon-eye-open' : 'icon-eye-close'"
                @click="togglePassword"
              ></i>
            </div>
          </div>
          
          <div class="form-options">
            <label class="checkbox-wrapper">
              <input type="checkbox" v-model="rememberMe">
              <span class="checkmark"></span>
              记住密码
            </label>
          </div>
          
          <button type="submit" class="login-btn" :disabled="isLoading">
            <span v-if="isLoading" class="loading-spinner"></span>
            {{ isLoading ? '登录中...' : '登录系统' }}
          </button>
        </form>
        
        <!-- 错误提示 -->
        <div v-if="errorMessage" class="error-message">
          <i class="icon-warning"></i>
          {{ errorMessage }}
        </div>
        
        <!-- 默认账号提示 -->
        <div class="default-account">
          <p class="hint-text">默认管理员账号</p>
          <p class="account-info">用户名: admin &nbsp;&nbsp; 密码: admin123</p>
        </div>
      </div>
    </div>
    
    <!-- 底部信息 -->
    <div class="login-footer">
      <p class="copyright">© 2025 电力负荷预测系统 - 基于机器学习学习的智能预测平台</p>
      <!-- <p class="tech-info">技术支持: Vue.js + FastAPI + PyTorch + MySQL</p> -->
    </div>
  </div>
</template>

<script>
import { authAPI } from '@/utils/api'
import { errorHandler } from '@/utils/errorHandler'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      showPassword: false,
      rememberMe: false,
      isLoading: false,
      errorMessage: ''
    }
  },
  mounted() {
    // 检查是否有记住的登录信息
    const savedUsername = localStorage.getItem('saved_username')
    const savedPassword = localStorage.getItem('saved_password')
    if (savedUsername && savedPassword) {
      this.loginForm.username = savedUsername
      this.loginForm.password = savedPassword
      this.rememberMe = true
    }
  },
  methods: {
    togglePassword() {
      this.showPassword = !this.showPassword
    },
    
    async handleLogin() {
      if (!this.loginForm.username || !this.loginForm.password) {
        this.errorMessage = '请输入用户名和密码'
        return
      }
      
      this.isLoading = true
      this.errorMessage = ''
      
      try {
        const response = await authAPI.login(this.loginForm)
        console.log('🔍 登录响应:', response)
        
        if (response.status === 'success') {
          // 存储token
          localStorage.setItem('access_token', response.data.access_token)
          console.log('🔍 存储的token:', response.data.access_token)
          console.log('🔍 验证token存储:', localStorage.getItem('access_token'))
          // 如果选择记住密码
          if (this.rememberMe) {
            localStorage.setItem('saved_username', this.loginForm.username)
            localStorage.setItem('saved_password', this.loginForm.password)
          } else {
            localStorage.removeItem('saved_username')
            localStorage.removeItem('saved_password')
          }
          this.$Message.success('登录成功')
          // 跳转到dashboard
          this.$router.replace('/dashboard')
        }
      } catch (error) {
        // 使用统一错误处理
        const errorInfo = errorHandler.handleError(error, 'Login.handleLogin', {
          showNotification: false // 我们自己处理显示
        })
        this.errorMessage = errorInfo.message
      } finally {
        this.isLoading = false
      }
    }
  }
}
</script>

<style lang="less" scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #0c1426 0%, #1a2332 50%, #0f1b2a 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  
  .background-decoration {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    
    .grid-lines {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-image: 
        linear-gradient(rgba(117, 222, 239, 0.1) 1px, transparent 1px),
        linear-gradient(90deg, rgba(117, 222, 239, 0.1) 1px, transparent 1px);
      background-size: 50px 50px;
      animation: gridMove 20s linear infinite;
    }
    
    .floating-particles {
      position: absolute;
      width: 100%;
      height: 100%;
      
      .particle {
        position: absolute;
        width: 4px;
        height: 4px;
        background: #75deef;
        border-radius: 50%;
        animation: float 6s infinite linear;
        opacity: 0.6;
      }
    }
  }
  
  .login-header {
    text-align: center;
    margin-bottom: 40px;
    z-index: 10;
    position: relative;
    
    .system-title {
      font-size: 32px;
      font-weight: bold;
      background: linear-gradient(45deg, #75deef, #4ecdc4, #ff6b6b);
      -webkit-background-clip: text;
      background-clip: text;
      -webkit-text-fill-color: transparent;
      margin: 0 0 10px 0;
      text-shadow: 0 0 30px rgba(117, 222, 239, 0.5);
    }
    
    .system-subtitle {
      font-size: 16px;
      color: #75deef;
      margin: 0;
      opacity: 0.8;
      letter-spacing: 2px;
    }
  }
  
  .login-form-wrapper {
    z-index: 10;
    position: relative;
    
    .login-form {
      background: rgba(7, 19, 50, 0.9);
      border: 1px solid #75deef;
      border-radius: 15px;
      padding: 40px;
      width: 400px;
      box-shadow: 
        0 0 30px rgba(117, 222, 239, 0.3),
        inset 0 0 20px rgba(7, 19, 50, 0.8);
      backdrop-filter: blur(10px);
      
      .form-header {
        text-align: center;
        margin-bottom: 30px;
        
        .login-icon {
          width: 60px;
          height: 60px;
          border: 2px solid #75deef;
          border-radius: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          margin: 0 auto 15px;
          background: rgba(117, 222, 239, 0.1);
          
          .icon-user {
            width: 24px;
            height: 24px;
            background: #75deef;
            mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z'/%3E%3C/svg%3E") no-repeat center/contain;
          }
        }
        
        .form-title {
          color: #75deef;
          font-size: 24px;
          margin: 0;
          font-weight: 300;
        }
      }
      
      .input-group {
        margin-bottom: 20px;
        
        .input-wrapper {
          position: relative;
          
          .input-icon {
            position: absolute;
            left: 15px;
            top: 50%;
            transform: translateY(-50%);
            width: 16px;
            height: 16px;
            background: #75deef;
            z-index: 2;
            
            &.icon-username {
              mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z'/%3E%3C/svg%3E") no-repeat center/contain;
            }
            
            &.icon-password {
              mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M18 8h-1V6c0-2.76-2.24-5-5-5S7 3.24 7 6v2H6c-1.1 0-2 .9-2 2v10c0 1.1.9 2 2 2h12c1.1 0 2-.9 2-2V10c0-1.1-.9-2-2-2zm-6 9c-1.1 0-2-.9-2-2s.9-2 2-2 2 .9 2 2-.9 2-2 2zm3.1-9H8.9V6c0-1.71 1.39-3.1 3.1-3.1 1.71 0 3.1 1.39 3.1 3.1v2z'/%3E%3C/svg%3E") no-repeat center/contain;
            }
          }
          
          .form-input {
            width: 100%;
            height: 50px;
            background: rgba(117, 222, 239, 0.05);
            border: 1px solid rgba(117, 222, 239, 0.3);
            border-radius: 8px;
            color: #fff;
            font-size: 16px;
            padding: 0 45px 0 45px;
            box-sizing: border-box;
            transition: all 0.3s ease;
            
            &:focus {
              outline: none;
              border-color: #75deef;
              box-shadow: 0 0 15px rgba(117, 222, 239, 0.3);
              background: rgba(117, 222, 239, 0.1);
            }
            
            &::placeholder {
              color: rgba(255, 255, 255, 0.5);
            }
          }
          
          .toggle-password {
            position: absolute;
            right: 15px;
            top: 50%;
            transform: translateY(-50%);
            width: 16px;
            height: 16px;
            background: rgba(117, 222, 239, 0.6);
            cursor: pointer;
            transition: all 0.3s ease;
            
            &:hover {
              background: #75deef;
            }
            
            &.icon-eye-close {
              mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 7c2.76 0 5 2.24 5 5 0 .65-.13 1.26-.36 1.83l2.92 2.92c1.51-1.26 2.7-2.89 3.43-4.75-1.73-4.39-6-7.5-11-7.5-1.4 0-2.74.25-3.98.7l2.16 2.16C10.74 7.13 11.35 7 12 7zM2 4.27l2.28 2.28.46.46C3.08 8.3 1.78 10.02 1 12c1.73 4.39 6 7.5 11 7.5 1.55 0 3.03-.3 4.38-.84l.42.42L19.73 22 21 20.73 3.27 3 2 4.27zM7.53 9.8l1.55 1.55c-.05.21-.08.43-.08.65 0 1.66 1.34 3 3 3 .22 0 .44-.03.65-.08l1.55 1.55c-.67.33-1.41.53-2.2.53-2.76 0-5-2.24-5-5 0-.79.2-1.53.53-2.2zm4.31-.78l3.15 3.15.02-.16c0-1.66-1.34-3-3-3l-.17.01z'/%3E%3C/svg%3E") no-repeat center/contain;
            }
            
            &.icon-eye-open {
              mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 4.5C7 4.5 2.73 7.61 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c-1.73-4.39-6-7.5-11-7.5zM12 17c-2.76 0-5-2.24-5-5s2.24-5 5-5 5 2.24 5 5-2.24 5-5 5zm0-8c-1.66 0-3 1.34-3 3s1.34 3 3 3 3-1.34 3-3-1.34-3-3-3z'/%3E%3C/svg%3E") no-repeat center/contain;
            }
          }
        }
      }
      
      .form-options {
        margin-bottom: 25px;
        
        .checkbox-wrapper {
          display: flex;
          align-items: center;
          color: rgba(255, 255, 255, 0.7);
          font-size: 14px;
          cursor: pointer;
          
          input[type="checkbox"] {
            display: none;
            
            &:checked + .checkmark {
              background: #75deef;
              border-color: #75deef;
              
              &::after {
                display: block;
              }
            }
          }
          
          .checkmark {
            width: 16px;
            height: 16px;
            border: 1px solid rgba(117, 222, 239, 0.5);
            border-radius: 3px;
            margin-right: 8px;
            position: relative;
            transition: all 0.3s ease;
            
            &::after {
              content: '';
              position: absolute;
              left: 5px;
              top: 2px;
              width: 4px;
              height: 8px;
              border: solid white;
              border-width: 0 2px 2px 0;
              transform: rotate(45deg);
              display: none;
            }
          }
        }
      }
      
      .login-btn {
        width: 100%;
        height: 50px;
        background: linear-gradient(45deg, #75deef, #4ecdc4);
        border: none;
        border-radius: 8px;
        color: #0c1426;
        font-size: 16px;
        font-weight: bold;
        cursor: pointer;
        transition: all 0.3s ease;
        display: flex;
        align-items: center;
        justify-content: center;
        
        &:hover:not(:disabled) {
          background: linear-gradient(45deg, #4ecdc4, #75deef);
          box-shadow: 0 0 20px rgba(117, 222, 239, 0.5);
          transform: translateY(-2px);
        }
        
        &:disabled {
          opacity: 0.6;
          cursor: not-allowed;
        }
        
        .loading-spinner {
          width: 20px;
          height: 20px;
          border: 2px solid transparent;
          border-top: 2px solid #0c1426;
          border-radius: 50%;
          animation: spin 1s linear infinite;
          margin-right: 10px;
        }
      }
      
      .error-message {
        margin-top: 15px;
        padding: 10px;
        background: rgba(255, 107, 107, 0.1);
        border: 1px solid #ff6b6b;
        border-radius: 6px;
        color: #ff6b6b;
        font-size: 14px;
        display: flex;
        align-items: center;
        
        .icon-warning {
          width: 16px;
          height: 16px;
          background: #ff6b6b;
          mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z'/%3E%3C/svg%3E") no-repeat center/contain;
          margin-right: 8px;
        }
      }
      
      .default-account {
        margin-top: 20px;
        padding: 15px;
        background: rgba(117, 222, 239, 0.05);
        border: 1px dashed rgba(117, 222, 239, 0.3);
        border-radius: 6px;
        text-align: center;
        
        .hint-text {
          color: rgba(255, 255, 255, 0.6);
          font-size: 12px;
          margin: 0 0 5px 0;
        }
        
        .account-info {
          color: #75deef;
          font-size: 14px;
          margin: 0;
          font-family: 'Courier New', monospace;
        }
      }
    }
  }
  
  .login-footer {
    position: absolute;
    bottom: 20px;
    text-align: center;
    z-index: 10;
    
    .copyright {
      color: rgba(255, 255, 255, 0.6);
      font-size: 14px;
      margin: 0 0 5px 0;
    }
    
    .tech-info {
      color: rgba(117, 222, 239, 0.7);
      font-size: 12px;
      margin: 0;
    }
  }
}

@keyframes gridMove {
  0% { transform: translate(0, 0); }
  100% { transform: translate(50px, 50px); }
}

@keyframes float {
  0%, 100% { 
    transform: translateY(100vh) rotate(0deg);
    opacity: 0;
  }
  10%, 90% { 
    opacity: 0.6; 
  }
  50% { 
    opacity: 1; 
  }
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>
